package com.ruoyi.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.app.vo.LoginUserVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.sms.domain.SmsConfig;
import com.ruoyi.sms.service.ISmsConfigService;
// import com.ruoyi.sms.utils.SendMsgUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/api")
public class AppLoginController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IAppUserService appUserService;

    // @Autowired
    // private SendMsgUtils sendMsgUtils;

    @Autowired
    private ISmsConfigService smsConfigService;
    @Autowired
    private RedisCache redisCache;


    /**
     * 登录方法
     *
     * @param map 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginUserVo map) {
        AjaxResult ajax = AjaxResult.success();
        // 设置一下登录时间
        // 无论是手机号校验，还是账号密码校验，如果为true，走底下的createToken方法
        HashMap res = appUserService.login(map);
        if ((boolean)res.get("isLogin") == true) {
            LoginUser loginBody = (LoginUser) res.get("loginBody");
            loginBody.setLoginTime(new Date().getTime());
            String token = tokenService.createToken(loginBody);
            ajax.put(Constants.TOKEN, token);
            ajax.put("appUser", res.get("appUser"));
            ajax.put("isNew", res.get("isNew"));
            return ajax;
        } else {
            return AjaxResult.error((String) res.get("msg"));
        }
    }

    /**
     * 注册
     *
     * @return 结果
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody LoginUserVo map) {
        AjaxResult ajax = AjaxResult.success();
        // 设置一下登录时间
        // 无论是手机号校验，还是账号密码校验，如果为true，走底下的createToken方法
        HashMap res = appUserService.register(map);
        if ((boolean)res.get("isLogin") == true) {
            LoginUser loginBody = (LoginUser) res.get("loginBody");
            loginBody.setLoginTime(new Date().getTime());
            String token = tokenService.createToken(loginBody);
            ajax.put(Constants.TOKEN, token);
            ajax.put("appUser", res.get("appUser"));
            return ajax;
        } else {
            return AjaxResult.error((String) res.get("msg"));
        }
    }

    /**
     * 发送短信
     *
     * @param phone 手机号
     * @return 结果
     */
    @GetMapping("/sendMsg")
    public AjaxResult sendMsg(@RequestParam String phone) {
        QueryWrapper<SmsConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("chosen", "Y");
        SmsConfig sysSmsConfig= smsConfigService.getOne(wrapper);
        // String msg = sendMsgUtils.sendVerificationCode(sysSmsConfig,phone);
        // if ("请稍后再试，冷却时间未到".equals(msg)) {
        //     return AjaxResult.error(msg);
        // }
        return AjaxResult.success();
    }

    /**
     * 微信登录App授权登录
     */
    @ApiOperation(value = "微信登录App授权登录")
    @RequestMapping(value = "/wx/login", method = RequestMethod.POST)
    public AjaxResult wxLogin(@RequestBody LoginUserVo map) {
        AjaxResult ajax = AjaxResult.success();
        HashMap<String, Object> result = appUserService.wxLogin(map);
        if (result.get("msg") != null) {
            return AjaxResult.error(result.get("msg").toString());
        }
        LoginUser loginBody = (LoginUser) result.get("loginBody");
        AppUser data = (AppUser) result.get("appUser");
        if (data != null) {
            loginBody.setLoginTime(new Date().getTime());
            String token = tokenService.createToken(loginBody);
            ajax.put(Constants.TOKEN, token);
            ajax.put("appUser", result.get("appUser"));
        }
        return ajax;
    }

}
